﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PizzaSystem
{
    public partial class UserApp : Form
    {
        public UserApp()
        {
            InitializeComponent();
        }
        public UserApp(AppHome app, Custom custom)
        {
            InitializeComponent();
            this.app = app;
            this.custom = custom;
            getPizza();
        }

        AppHome app;

        Custom custom;

        SqlDao sqlDao = new SqlDao(); 

        List<Pizza> list;

        private void getPizza()
        {
            list = sqlDao.selectPizza();
            if(list.Count > 0)
            {
                price.Text = list[0].P_price.ToString();
                name.Text = list[0].P_name.ToString();
                name.DataSource = list;
                name.DisplayMember = "p_name";
                name.ValueMember = "p_price";
            }
        }

        bool e2 = true;
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if(e.TabPageIndex == 0)
            {
                getPizza();
            }
            else if(e.TabPageIndex == 1)
            {
                DataSet ds = sqlDao.selectBillById(custom.C_id, 0);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView1.DataSource = ds.Tables[0].DefaultView;
                    if (e2)
                    {
                        DataGridViewButtonColumn column = new DataGridViewButtonColumn();
                        column.Text = "退 订";
                        column.Name = "编 辑";
                        column.UseColumnTextForButtonValue = true;
                        dataGridView1.Columns.Add(column);
                        e2 = false;
                    }
                }
            }
            else if(e.TabPageIndex == 2)
            {
                u_id.Text = custom.C_id.ToString();
                u_name.Text = custom.C_name.ToString();
                addr.Text = custom.C_addr.ToString();
                account.Text = custom.C_account.ToString();
                tel.Text = custom.C_tel.ToString();
            }
            else
            {
                if (MessageBox.Show("确定要退出？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    app.openForm(new Home(app));
            }
        }

        bool f = true;
        private void name_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (f)
            {
                f = false;
                return;
            }
            price.Text = name.SelectedValue.ToString();
            t_price.Text = (decimal.Parse(price.Text) * int.Parse(num.Text)).ToString("0.00");
        }
        private void num_TextChanged(object sender, EventArgs e)
        {
            if (num.Text == "")
            {
                t_price.Text = "0.00";
                return;
            }
            if (num.Text[num.Text.Length - 1] >= '0' && num.Text[num.Text.Length - 1] <= '9')
            {
                if (long.Parse(num.Text) > 1000000)
                {
                    MessageBox.Show("一次最多只能订购1000000份");
                    num.Text = "1000000";
                    return;
                }
                t_price.Text = (decimal.Parse(price.Text) * int.Parse(num.Text)).ToString("0.00");
            }
            else
            {
                num.Text = num.Text.Remove(num.Text.Length - 1);
            }
        }
        private void submit_Click(object sender, EventArgs e)
        {
            if (num.Text == "0"||num.Text=="")
            {
                MessageBox.Show("请先选择订购数量...");
                return;
            }
            DateTime date = DateTime.Now;
            if (date.AddDays(1) > DateTime.Parse(time.Text) && long.Parse(num.Text) >= 1000)
            {
                MessageBox.Show("交付时间至少为1天后...");
                return;
            }
            else if (date.AddDays(3) > DateTime.Parse(time.Text) && long.Parse(num.Text) >= 10000)
            {
                MessageBox.Show("交付时间至少为3天后...");
                return;
            }
            else if (date.AddDays(5) > DateTime.Parse(time.Text) && long.Parse(num.Text) >= 100000)
            {
                MessageBox.Show("交付时间至少为5天后...");
                return;
            }
            else if (date.AddDays(10) > DateTime.Parse(time.Text) && long.Parse(num.Text) >= 500000)
            {
                MessageBox.Show("交付时间至少为10天后...");
                return;
            }
            else if (date.AddDays(30) > DateTime.Parse(time.Text) && long.Parse(num.Text) >= 1000000)
            {
                MessageBox.Show("交付时间至少为30天后...");
                return;
            }

            Bill bill = new Bill();
            bill.B_id = Utils.getTimeTicks();
            bill.B_time = DateTime.Parse(time.Text).ToString("yyyy-MM--dd HH:mm:ss");
            bill.B_money = decimal.Parse(t_price.Text);
            bill.B_num = int.Parse(num.Text);
            bill.C_id = custom.C_id;
            foreach (var item in list)
            {
                if (decimal.Parse(price.Text) == item.P_price &&
                    name.Text == item.P_name)
                {
                    bill.P_id = item.P_id;
                }
            }
            if(sqlDao.insertBill(bill))
            {
                MessageBox.Show("订购成功...");
            }
            else
            {
                MessageBox.Show("订购失败...");
            }
        }

        //订单管理
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 0)
            {
                return;
            }
            if (MessageBox.Show("确定要退订？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string b_id = dataGridView1.Rows[e.RowIndex].Cells["订单号"].Value.ToString();
                if (sqlDao.updateBillState(b_id, custom.C_id, -1))
                {
                    MessageBox.Show("退订成功...");
                    dataGridView1.Rows.RemoveAt(e.RowIndex);
                }
                else
                {
                    MessageBox.Show("退订失败...");
                }
            }
        }

        private void update_Click(object sender, EventArgs e)
        {
            if (u_name.Text == "" || tel.Text == ""|| addr.Text=="")
            {
                MessageBox.Show("号码和姓名、地址不能为空...");
            }
            else
            {
                if (MessageBox.Show("确定要修改？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    custom.C_tel = tel.Text;
                    custom.C_name = u_name.Text;
                    custom.C_addr = addr.Text;
                    if (sqlDao.updateCustom(custom))
                    {
                        MessageBox.Show("修改成功...");
                    }
                    else
                    {
                        MessageBox.Show("修改失败...");
                    }
                }
            }
        }
    }
}
